﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace SQLDAL
{
    /// <summary>
    /// 数据访问类：User
    /// </summary>
    public partial class User
    {
        public User() { }
        #region Method
        /// <summary>
        /// 判断用户名、密码是否正确
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="userPassword"></param>
        /// <returns></returns>
        public bool Login(string userName,string userPassword)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select count(*) from [User]");
            sb.AppendFormat(" where UserName = @username and Password = @password");
            SqlParameter[] parameters =
            {
                new SqlParameter("@username",SqlDbType.VarChar,50),
                new SqlParameter("@password",SqlDbType.VarChar,50)
            };
            parameters[0].Value = userName;
            parameters[1].Value = userPassword;
            int n = (int)SqlDbHelper.ExecuteScalar(sb.ToString(), CommandType.Text, parameters);
            if (n == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新用户密码
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool UpdateUserPassword(Model.User user)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("update [user] set ");
            sb.Append(" password  =@password");
            sb.Append(" where username = @username");
            SqlParameter[] parameters =
            {
                new SqlParameter("@password",SqlDbType.VarChar,50),
                new SqlParameter("@username",SqlDbType.VarChar,50)
            };
            parameters[0].Value = user.Password;
            parameters[1].Value = user.UserName;
            int rows = SqlDbHelper.ExecuteNonQuery(sb.ToString(), CommandType.Text, parameters);
            if (rows == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion Method
    }
}
